import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_news/common/entity/entitys.dart';
import 'package:flutter_news/common/provider/provider.dart';
import 'package:flutter_news/common/utils/utils.dart';
import 'package:flutter_news/common/values/values.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:device_info_plus/device_info_plus.dart';

/// 全局配置
class Global {
  /// 用户配置
  static UserLoginResponseEntity? profile = UserLoginResponseEntity(
    accessToken: "",
    channels: [],
    displayName: '',
  );

  /// 发布渠道
  static String channel = "xiaomi";

  /// 是否 ios
  static bool isIOS = Platform.isIOS;

  /// android 设备信息
  static late AndroidDeviceInfo androidDeviceInfo;

  /// ios 设备信息
  static late IosDeviceInfo iosDeviceInfo;

  /// 包信息
  static late PackageInfo packageInfo;

  /// 是否 release
  static bool get isRelease => bool.fromEnvironment("dart.vm.product");

  /// 是否第一次打开
  static bool isFirstOpen = false;

  /// 是否离线登录
  static bool isOfflineLogin = false;

  /// 应用状态
  static AppState appState = AppState();

  /// init
  static Future init() async {
    // 运行初始
    WidgetsFlutterBinding.ensureInitialized();

    packageInfo = await PackageInfo.fromPlatform();
    DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    if (isIOS) {
      iosDeviceInfo = await deviceInfoPlugin.iosInfo;
    } else {
      androidDeviceInfo = await deviceInfoPlugin.androidInfo;
    }

    // 工具初始
    await StorageUtil.init();
    HttpUtil();

    // 读取设备第一次打开
    isFirstOpen = !StorageUtil().getBool(STORAGE_DEVICE_ALREADY_OPEN_KEY);
    if (isFirstOpen) {
      StorageUtil().setBool(STORAGE_DEVICE_ALREADY_OPEN_KEY, true);
    }

    // 读取离线用户信息
    var _profileJSON = StorageUtil().getJson(STORAGE_USER_PROFILE_KEY);
    if (_profileJSON != null) {
      profile = UserLoginResponseEntity.fromJson(_profileJSON);
      isOfflineLogin = true;
    }

    // http 缓存

    // android 状态栏为透明的沉浸
    if (Platform.isAndroid) {
      // 这里逻辑可以放到 MaterialApp 的 builder: (BuildContext context, Widget child){} 方法中
      /**
       * 例如：
       * class MyApp extends StatelessWidget {
          @override
          Widget build(BuildContext context) {
          // 使用MaterialApp提供的builder属性来设置状态栏样式
          return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
          primarySwatch: Colors.blue,
          ),
          builder: (BuildContext context, Widget child) {
          // 设置状态栏的样式为亮色
          SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
          statusBarColor: Colors.transparent, // 可选，设置状态栏背景颜色
          statusBarBrightness: Brightness.light, // 设置状态栏文案颜色为亮色
          statusBarIconBrightness: Brightness.dark, // 设置状态栏图标颜色为暗色
          ));
          return child;
          },
          home: MyHomePage(),
          );
          }
          }
       */
      SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        // 可选，设置状态栏背景颜色
        statusBarBrightness: Brightness.dark,
        // Brightness.light 设置状态栏文案颜色为亮色 (测试不生效，可能部分机型生效)
        statusBarIconBrightness:
            Brightness.dark, // Brightness.dark 设置状态栏图标颜色为暗色（这个文案和图标都可以变色）
      );
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }
  }

  // 持久化 用户信息
  static Future<bool> saveProfile(UserLoginResponseEntity userResponse) {
    profile = userResponse;
    return StorageUtil()
        .setJson(STORAGE_USER_PROFILE_KEY, userResponse.toJson());
  }
}
